unit Dos.ORM.Entity;

interface

uses
  Dos.ORM.EnumHelper, Dos.ORM.Field, System.Classes, System.Generics.Collections;

type
  TModifyField = class
  private
    FoldValue, FnewValue: TObject;
    Ffield: TObject;
  public
    property oldValue: TObject read FoldValue write FoldValue;
    property newValue: TObject read FnewValue write FnewValue;
    procedure ModifyField(field: TObject; aoldValue, anewValue: TObject);
  end;
  TObjects=array of TObject;
  Entity = class
  private
    FTableName: string;
    FIsAttached: boolean;
    FEntityState: EntityState;
    FModifyFields: TList<TModifyField>;
  private
    constructor Create(); overload;
    constructor Create(tablename: string); overload;
    procedure Attach(); overload;
    procedure DeAttach();
    procedure Attach(entityState: EntityState); overload;
    function GetEntityState(): EntityState;
    procedure OnPropertyValueChange(Field: TField; oldValue, newValue: TObject);
    procedure ClearModifyFields();
    function GetFields():TFields;virtual;abstract;
    function GetValues():TObjects;virtual;abstract;
    function GetPrimaryKeyFields():TFields;virtual; abstract;
    function GetIdentityField():TField;virtual;abstract;
    function GetModifyFields():TList<TModifyField>;
    function IsReadOnly():Boolean;virtual;abstract;
    function GetTableName():string;
  end;

implementation

{ ModifyField }

procedure TModifyField.ModifyField(field, aoldValue, anewValue: TObject);
begin
  Ffield := field;
  FoldValue := aoldValue;
  FnewValue := anewValue;
end;

{ Entity }

procedure Entity.Attach(entityState: EntityState);
begin
  self.FEntityState := entityState;
end;

procedure Entity.Attach;
begin
  self.FIsAttached := true;
end;

constructor Entity.Create;
begin
  Self.FIsAttached := True;
end;

procedure Entity.ClearModifyFields;
begin
  FmodifyFields.Clear;
end;

constructor Entity.Create(tablename: string);
begin
  self.FTableName := tablename;
  Self.FIsAttached := True;
end;

procedure Entity.DeAttach;
begin
  self.FisAttached := false;
  self.FentityState := EntityState.Unchanged;
end;

function Entity.GetEntityState: EntityState;
begin
  Result := Self.FEntityState;
end;


function Entity.GetModifyFields: TList<TModifyField>;
begin
  Result:=self.FModifyFields;
end;

function Entity.GetTableName: string;
begin
  Result:=Self.FTableName;
end;



procedure Entity.OnPropertyValueChange(Field: TField; oldValue, newValue: TObject);
begin
  if FIsAttached then
  begin

  end;

end;

end.

